﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    /// <summary>
    /// Classe que cria um objeto que desenha os eixos 3d na tela
    /// </summary>
    class GameObject 
    {
        /// <summary>
        /// dispositivo gráfico
        /// </summary>
        GraphicsDevice device;

        /// <summary>
        /// Conjunto de vértices que será desenhdo na tela.
        /// </summary>
        VertexBuffer vertexBuffer;

        /// <summary>
        /// Efeito que será utilizado para fazer o desenho 3d
        /// </summary>
        BasicEffect effect;

        /// <summary>
        /// Matriz que representa as transformações do mundo virtual
        /// </summary>
        Matrix rot = Matrix.Identity, scale = Matrix.Identity, trans = Matrix.Identity;

        /// <summary>
        /// Variáveis para atualização do mouse.
        /// </summary>
        MouseState mouseAtual, mouseAnterior;
        /// <summary>
        /// Variáveis para atualização do mouse.
        /// </summary>
        KeyboardState tecladoAtual, tecladoAnterior;

        /// <summary>
        /// Modelo 3D Motor da classe.
        /// </summary>
        Model modelo;

        Video video, video2;
        VideoPlayer player, player2;

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="device">Dispositivo gráfico</param>
        public GameObject(GraphicsDevice gDevice, ContentManager Content )
        {
            device = gDevice;

            modelo = Content.Load<Model>("caixa");

            video = Content.Load<Video>("video");
            player = new VideoPlayer();
            player.IsLooped = true;
            player.Play(video);

            video2 = Content.Load<Video>("video2");
            player2 = new VideoPlayer();
            player2.IsLooped = true;
            player2.Play(video);

            effect = modelo.Meshes[0].Effects[0] as BasicEffect;

            //effect.View = Matrix.CreateLookAt(new Vector3(1.0f, 2.0f, 3.0f), Vector3.Zero, Vector3.Up);
            //effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), device.Viewport.AspectRatio, 1.0f, 10.0f);
            effect.EnableDefaultLighting();

//            Create3DAxis();

        }

        /// <summary>
        /// Limpa os vortex buffer (array de vertices na memoria de vídeo)
        /// </summary>
        public void UnloadContent()
        {

        }

        /// <summary>
        /// Cria o eixo 3D para ser desenhado na tela.
        /// </summary>
        private void Create3DAxis()
        {
            float axisLength = 1f;
            int vertexCount = 6;

            VertexPositionColor[] vertice = new VertexPositionColor[vertexCount];

            vertice[0] = new VertexPositionColor(new Vector3(-axisLength, 0.0f, 0.0f), Color.Red);
            vertice[1] = new VertexPositionColor(new Vector3(axisLength, 0.0f, 0.0f), Color.Red);

            vertice[2] = new VertexPositionColor(new Vector3(0.0f, -axisLength, 0.0f), Color.Green);
            vertice[3] = new VertexPositionColor(new Vector3(0.0f, axisLength, 0.0f), Color.Green);
            
            vertice[4] = new VertexPositionColor(new Vector3(0.0f, 0.0f, -axisLength), Color.Blue);
            vertice[5] = new VertexPositionColor(new Vector3(0.0f, 0.0f, axisLength), Color.Blue);

            vertexBuffer = new VertexBuffer(device,VertexPositionColor.VertexDeclaration,vertexCount,BufferUsage.WriteOnly);

            vertexBuffer.SetData<VertexPositionColor>(vertice);
        }

        public void escala(float valor)
        {
            scale *= Matrix.CreateScale(valor);
        }

        public void transladar(float valor)
        {
            trans *= Matrix.CreateTranslation(new Vector3(valor,valor,0.0f));
        }

        /// <summary>
        /// Função de Atualização.
        /// </summary>
        /// <param name="gameTime">Tempo controlador do jogo</param>
        public void Update(GameTime gameTime)
        {
            mouseAtual = Mouse.GetState();
            tecladoAtual = Keyboard.GetState();            

            if (mouseAtual.LeftButton == ButtonState.Pressed)
            {
                rot *= Matrix.CreateRotationX(MathHelper.ToRadians(1f)) * Matrix.CreateRotationY(MathHelper.ToRadians(1f));
            }

            if (tecladoAtual.IsKeyDown(Keys.PageUp) && !tecladoAnterior.IsKeyDown(Keys.PageUp))
            {
                scale *= Matrix.CreateScale(0.9f);
            }
            else if (tecladoAtual.IsKeyDown(Keys.PageDown) && !tecladoAnterior.IsKeyDown(Keys.PageDown))
            {
                scale *= Matrix.CreateScale(1.2f);
            }

            if (tecladoAtual.IsKeyDown(Keys.Right))
            {
                trans *= Matrix.CreateTranslation(new Vector3(0.01f, 0.0f, 0.0f));
            }
            else if (tecladoAtual.IsKeyDown(Keys.Left))
            {
                trans *= Matrix.CreateTranslation(new Vector3(-0.01f, 0.0f, 0.0f));
            }

            if (tecladoAtual.IsKeyDown(Keys.Up))
            {
                trans *= Matrix.CreateTranslation(new Vector3(0.0f, 0.01f, 0.0f));
            }
            else if (tecladoAtual.IsKeyDown(Keys.Down))
            {
                trans *= Matrix.CreateTranslation(new Vector3(0.0f, -0.01f, 0.0f));
            }

            mouseAnterior = mouseAtual;
            tecladoAnterior = tecladoAtual;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Draw(Camera cam)
        {

            effect.EnableDefaultLighting();
            effect.TextureEnabled = true;
            effect.Texture = player.GetTexture();

            modelo.Draw(
                rot * scale * trans,
                cam.view,
                cam.projection);

            /*foreach (EffectPass CurrentPass in effect.CurrentTechnique.Passes)
            {
                CurrentPass.Apply();
                device.DrawPrimitives(PrimitiveType.LineList, 0, 3);
            }*/
        }
    }
}
